package spem.activity.diagram.providers;

import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;

import spem.SpemPackage;
import spem.activity.diagram.edit.parts.Activity2EditPart;
import spem.activity.diagram.edit.parts.ActivityEditPart;
import spem.activity.diagram.edit.parts.ControlFlowEditPart;
import spem.activity.diagram.edit.parts.DecisionEditPart;
import spem.activity.diagram.edit.parts.RoleDefinitionEditPart;
import spem.activity.diagram.edit.parts.RoleDefinitionPerformsEditPart;
import spem.activity.diagram.edit.parts.Step2EditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionEditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionInputEditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionOutputEditPart;
import spem.activity.diagram.edit.parts.TaskDefinitionUsesEditPart;
import spem.activity.diagram.edit.parts.ToolEditPart;
import spem.activity.diagram.edit.parts.WorkProductDefinitionEditPart;
import spem.activity.diagram.part.SpemDiagramEditorPlugin;

/**
 * @generated
 */
public class SpemElementTypes {

	/**
	 * @generated
	 */
	private SpemElementTypes() {
	}

	/**
	 * @generated
	 */
	private static Map<IElementType, ENamedElement> elements;

	/**
	 * @generated
	 */
	private static ImageRegistry imageRegistry;

	/**
	 * @generated
	 */
	private static Set<IElementType> KNOWN_ELEMENT_TYPES;

	/**
	 * @generated
	 */
	public static final IElementType Activity_1000 = getElementType("spemGraph.activity.diagram.Activity_1000"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType WorkProductDefinition_2001 = getElementType("spemGraph.activity.diagram.WorkProductDefinition_2001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType RoleDefinition_2002 = getElementType("spemGraph.activity.diagram.RoleDefinition_2002"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Activity_2003 = getElementType("spemGraph.activity.diagram.Activity_2003"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Decision_2004 = getElementType("spemGraph.activity.diagram.Decision_2004"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType TaskDefinition_2005 = getElementType("spemGraph.activity.diagram.TaskDefinition_2005"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Tool_2006 = getElementType("spemGraph.activity.diagram.Tool_2006"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Step_3001 = getElementType("spemGraph.activity.diagram.Step_3001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType ControlFlow_4001 = getElementType("spemGraph.activity.diagram.ControlFlow_4001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType RoleDefinitionPerforms_4002 = getElementType("spemGraph.activity.diagram.RoleDefinitionPerforms_4002"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType TaskDefinitionInput_4003 = getElementType("spemGraph.activity.diagram.TaskDefinitionInput_4003"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType TaskDefinitionUses_4004 = getElementType("spemGraph.activity.diagram.TaskDefinitionUses_4004"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType TaskDefinitionOutput_4005 = getElementType("spemGraph.activity.diagram.TaskDefinitionOutput_4005"); //$NON-NLS-1$

	/**
	 * @generated
	 */
	private static ImageRegistry getImageRegistry() {
		if (imageRegistry == null) {
			imageRegistry = new ImageRegistry();
		}
		return imageRegistry;
	}

	/**
	 * @generated
	 */
	private static String getImageRegistryKey(ENamedElement element) {
		return element.getName();
	}

	/**
	 * @generated
	 */
	private static ImageDescriptor getProvidedImageDescriptor(
			ENamedElement element) {
		if (element instanceof EStructuralFeature) {
			EStructuralFeature feature = ((EStructuralFeature) element);
			EClass eContainingClass = feature.getEContainingClass();
			EClassifier eType = feature.getEType();
			if (eContainingClass != null && !eContainingClass.isAbstract()) {
				element = eContainingClass;
			} else if (eType instanceof EClass
					&& !((EClass) eType).isAbstract()) {
				element = eType;
			}
		}
		if (element instanceof EClass) {
			EClass eClass = (EClass) element;
			if (!eClass.isAbstract()) {
				return SpemDiagramEditorPlugin.getInstance()
						.getItemImageDescriptor(
								eClass.getEPackage().getEFactoryInstance()
										.create(eClass));
			}
		}
		// TODO : support structural features
		return null;
	}

	/**
	 * @generated
	 */
	public static ImageDescriptor getImageDescriptor(ENamedElement element) {
		String key = getImageRegistryKey(element);
		ImageDescriptor imageDescriptor = getImageRegistry().getDescriptor(key);
		if (imageDescriptor == null) {
			imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(key, imageDescriptor);
		}
		return imageDescriptor;
	}

	/**
	 * @generated
	 */
	public static Image getImage(ENamedElement element) {
		String key = getImageRegistryKey(element);
		Image image = getImageRegistry().get(key);
		if (image == null) {
			ImageDescriptor imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(key, imageDescriptor);
			image = getImageRegistry().get(key);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public static ImageDescriptor getImageDescriptor(IAdaptable hint) {
		ENamedElement element = getElement(hint);
		if (element == null) {
			return null;
		}
		return getImageDescriptor(element);
	}

	/**
	 * @generated
	 */
	public static Image getImage(IAdaptable hint) {
		ENamedElement element = getElement(hint);
		if (element == null) {
			return null;
		}
		return getImage(element);
	}

	/**
	 * Returns 'type' of the ecore object associated with the hint.
	 * 
	 * @generated
	 */
	public static ENamedElement getElement(IAdaptable hint) {
		Object type = hint.getAdapter(IElementType.class);
		if (elements == null) {
			elements = new IdentityHashMap<IElementType, ENamedElement>();

			elements.put(Activity_1000, SpemPackage.eINSTANCE.getActivity());

			elements.put(WorkProductDefinition_2001,
					SpemPackage.eINSTANCE.getWorkProductDefinition());

			elements.put(RoleDefinition_2002,
					SpemPackage.eINSTANCE.getRoleDefinition());

			elements.put(Activity_2003, SpemPackage.eINSTANCE.getActivity());

			elements.put(Decision_2004, SpemPackage.eINSTANCE.getDecision());

			elements.put(TaskDefinition_2005,
					SpemPackage.eINSTANCE.getTaskDefinition());

			elements.put(Tool_2006, SpemPackage.eINSTANCE.getTool());

			elements.put(Step_3001, SpemPackage.eINSTANCE.getStep());

			elements.put(ControlFlow_4001,
					SpemPackage.eINSTANCE.getControlFlow());

			elements.put(RoleDefinitionPerforms_4002,
					SpemPackage.eINSTANCE.getRoleDefinition_Performs());

			elements.put(TaskDefinitionInput_4003,
					SpemPackage.eINSTANCE.getTaskDefinition_Input());

			elements.put(TaskDefinitionUses_4004,
					SpemPackage.eINSTANCE.getTaskDefinition_Uses());

			elements.put(TaskDefinitionOutput_4005,
					SpemPackage.eINSTANCE.getTaskDefinition_Output());
		}
		return (ENamedElement) elements.get(type);
	}

	/**
	 * @generated
	 */
	private static IElementType getElementType(String id) {
		return ElementTypeRegistry.getInstance().getType(id);
	}

	/**
	 * @generated
	 */
	public static boolean isKnownElementType(IElementType elementType) {
		if (KNOWN_ELEMENT_TYPES == null) {
			KNOWN_ELEMENT_TYPES = new HashSet<IElementType>();
			KNOWN_ELEMENT_TYPES.add(Activity_1000);
			KNOWN_ELEMENT_TYPES.add(WorkProductDefinition_2001);
			KNOWN_ELEMENT_TYPES.add(RoleDefinition_2002);
			KNOWN_ELEMENT_TYPES.add(Activity_2003);
			KNOWN_ELEMENT_TYPES.add(Decision_2004);
			KNOWN_ELEMENT_TYPES.add(TaskDefinition_2005);
			KNOWN_ELEMENT_TYPES.add(Tool_2006);
			KNOWN_ELEMENT_TYPES.add(Step_3001);
			KNOWN_ELEMENT_TYPES.add(ControlFlow_4001);
			KNOWN_ELEMENT_TYPES.add(RoleDefinitionPerforms_4002);
			KNOWN_ELEMENT_TYPES.add(TaskDefinitionInput_4003);
			KNOWN_ELEMENT_TYPES.add(TaskDefinitionUses_4004);
			KNOWN_ELEMENT_TYPES.add(TaskDefinitionOutput_4005);
		}
		return KNOWN_ELEMENT_TYPES.contains(elementType);
	}

	/**
	 * @generated
	 */
	public static IElementType getElementType(int visualID) {
		switch (visualID) {
		case ActivityEditPart.VISUAL_ID:
			return Activity_1000;
		case WorkProductDefinitionEditPart.VISUAL_ID:
			return WorkProductDefinition_2001;
		case RoleDefinitionEditPart.VISUAL_ID:
			return RoleDefinition_2002;
		case Activity2EditPart.VISUAL_ID:
			return Activity_2003;
		case DecisionEditPart.VISUAL_ID:
			return Decision_2004;
		case TaskDefinitionEditPart.VISUAL_ID:
			return TaskDefinition_2005;
		case ToolEditPart.VISUAL_ID:
			return Tool_2006;
		case Step2EditPart.VISUAL_ID:
			return Step_3001;
		case ControlFlowEditPart.VISUAL_ID:
			return ControlFlow_4001;
		case RoleDefinitionPerformsEditPart.VISUAL_ID:
			return RoleDefinitionPerforms_4002;
		case TaskDefinitionInputEditPart.VISUAL_ID:
			return TaskDefinitionInput_4003;
		case TaskDefinitionUsesEditPart.VISUAL_ID:
			return TaskDefinitionUses_4004;
		case TaskDefinitionOutputEditPart.VISUAL_ID:
			return TaskDefinitionOutput_4005;
		}
		return null;
	}

}
